package com.mofang.powerdekorhelper.activity.train;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ProgressBar;

import com.mofang.powerdekorhelper.R;
import com.mofang.powerdekorhelper.base.BaseActivity;
import com.mofang.powerdekorhelper.utils.L;
import com.mofang.powerdekorhelper.utils.SharePerforenceUtils;
import com.mofang.powerdekorhelper.utils.T;
import com.mofang.powerdekorhelper.witget.MyWebViewClient;
import com.mofang.powerdekorhelper.witget.TitleBar;

import butterknife.BindView;

/**
 * Created by luofei on 2017/12/28 0028.
 * 说明：
 */

public class TrainingSystemActivity extends BaseActivity {

    @BindView(R.id.training_system_title)
    TitleBar mTitleBar;

    @BindView(R.id.training_system_web)
    WebView mWebView;

    @BindView(R.id.training_system_web_pro)
    ProgressBar mProgressBar;

    private LocationManager locationManager;
    private String locationProvider;
    private Location location;

    double latitude;
    double longtitude;

    @Override
    public void initView() {
        inflateLayout(R.layout.activity_training_system);
        initTitleBar2(mTitleBar, R.string.training_system);
    }

    @Override
    public void initData() {

        int uerId = SharePerforenceUtils.getInstance(this).getUserID();

        if (!getLngAndLatWithNetwork()) {
            T.showShort(this, "抱歉,位置信息获取失败");
            return;
        }

        String baseUrl = "http://powerdekor.glscloud.com/pxclient?userid=" + uerId + "&gpsX=" + latitude + "&gpsY=" + longtitude;

        L.i("TrainingSystemActivity", "baseUrl------->" + baseUrl);

        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.setWebViewClient(new MyWebViewClient());
        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                //super.onProgressChanged(view, newProgress);
                if (newProgress == 100) {
                    mProgressBar.setVisibility(View.GONE);//加载完网页进度条消失
                } else {
                    mProgressBar.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
                    mProgressBar.setProgress(newProgress);//设置进度值
                }
            }
        });

        // 暂不加载网页
        //mWebView.loadUrl(baseUrl);
        //mWebView.loadDataWithBaseURL(null, baseUrl, "text/html", "utf-8", null);
    }

    //从网络获取经纬度
    public boolean getLngAndLatWithNetwork() {

        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return false;
        }

        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);
        Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        if (location != null) {
            latitude = location.getLatitude();
            longtitude = location.getLongitude();
            return true;
        }

        return false;
    }

    LocationListener locationListener = new LocationListener() {

        // Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        // Provider被enable时触发此函数，比如GPS被打开
        @Override
        public void onProviderEnabled(String provider) {

        }

        // Provider被disable时触发此函数，比如GPS被关闭
        @Override
        public void onProviderDisabled(String provider) {

        }

        //当坐标改变时触发此函数，如果Provider传进相同的坐标，它就不会被触发
        @Override
        public void onLocationChanged(Location location) {
        }
    };


}
